package com.example.fangkb;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button buttonNormal = (Button) findViewById(R.id.btn_dialog1);
        buttonNormal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showNormalDialog();
            }
        });
        Button buttonNormal1 =(Button) findViewById (R.id.btn_dialog2);
        buttonNormal1.setOnClickListener (new View.OnClickListener ( ) {
            @Override
            public void onClick(View v) {

                showMultiBtnDialog();
            }
        });
        Button buttonNormal2 =(Button) findViewById (R.id.btn_dialog3);
        buttonNormal2.setOnClickListener (new View.OnClickListener ( ) {
            @Override
            public void onClick(View v) {
                showSingleChoiceDialog();
            }
        });
        Button buttonNormal3 =(Button) findViewById (R.id.btn_dialog4);
        buttonNormal3.setOnClickListener (new View.OnClickListener ( ) {
            @Override
            public void onClick(View v) {
                showMultiChoiceDialog();
            }
        });
        Button buttonNormal4 =(Button) findViewById (R.id.btn_dialog5);
        buttonNormal4.setOnClickListener (new View.OnClickListener ( ) {
            @Override
            public void onClick(View v) {
                showWaitingDialog();
            }
        });
        Button buttonNormal5 =(Button) findViewById (R.id.btn_dialog6);
        buttonNormal5.setOnClickListener (new View.OnClickListener ( ) {
            @Override
            public void onClick(View v) {
                showProgressDialog();
            }
        });
        Button buttonNormal6 =(Button) findViewById (R.id.btn_dialog7);
        buttonNormal6.setOnClickListener (new View.OnClickListener ( ) {
            @Override
            public void onClick(View v) {
                showInputDialog();
            }
        }); Button buttonNormal7 =(Button) findViewById (R.id.btn_dialog8);
        buttonNormal7.setOnClickListener (new View.OnClickListener ( ) {
            @Override
            public void onClick(View v) {
                showCustomizeDialog();
            }
        });

        Button buttonNormal8 =(Button) findViewById (R.id.btn_dialog9);
        buttonNormal8.setOnClickListener (new View.OnClickListener ( ) {
            @Override
            public void onClick(View v) {
                showListDialog();
            }
        });
    }

    private void showNormalDialog(){
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(MainActivity.this);
        normalDialog.setIcon(R.mipmap.ic_launcher);
        normalDialog.setTitle("请问您需要甜品吗");
        normalDialog.setMessage("What can I do for you?");
        normalDialog.setPositiveButton("好的，谢谢",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
        normalDialog.setNegativeButton("不用，谢谢",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
        normalDialog.show();
    }
    private void showMultiBtnDialog(){
        AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(MainActivity.this);
        normalDialog.setIcon(R.mipmap.ic_launcher);
        normalDialog.setTitle("你会点哪个").setMessage("挂科会怎么样?");
        normalDialog.setPositiveButton("心痛",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
        normalDialog.setNeutralButton("没事",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
        normalDialog.setNegativeButton("无所谓", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        normalDialog.show();
    }
    int yourChoice;
    private void showSingleChoiceDialog(){
        final String[] items = { "刘亦菲","范冰冰","刘德华","张国荣" };
        yourChoice = -1;
        AlertDialog.Builder singleChoiceDialog =
                new AlertDialog.Builder(MainActivity.this);
        singleChoiceDialog.setTitle("选择你最喜欢的明星");
        singleChoiceDialog.setSingleChoiceItems (items,-1,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        yourChoice = which;
                    }
                });
        singleChoiceDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (yourChoice >= -1) {
                            Toast.makeText(MainActivity.this,
                                    "你最喜欢的明星是" + items[yourChoice],
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                });
        singleChoiceDialog.show();
    }

    ArrayList<Integer> yourChoices = new ArrayList<>();
    private void showMultiChoiceDialog() {
        final String[] items = { "刘亦菲","范冰冰","刘德华","张国荣" };
        final boolean initChoiceSets[]={false,false,false,false};
        yourChoices.clear();
        AlertDialog.Builder multiChoiceDialog =
                new AlertDialog.Builder(MainActivity.this);
        multiChoiceDialog.setTitle("你喜欢的明星有");
        multiChoiceDialog.setMultiChoiceItems(items, initChoiceSets,
                new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which,
                                        boolean isChecked) {
                        if (isChecked) {
                            yourChoices.add(which);
                        } else {
                            yourChoices.remove(which);
                        }
                    }
                });
        multiChoiceDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int size = yourChoices.size();
                        String str = "";
                        for (int i = 0; i < size; i++) {
                            str += items[yourChoices.get(i)] + " ";
                        }
                        Toast.makeText(MainActivity.this,
                                "你喜欢的明星有" + str,
                                Toast.LENGTH_SHORT).show();
                    }
                });
        multiChoiceDialog.show();
    }
    private void showWaitingDialog() {
        ProgressDialog waitingDialog=
                new ProgressDialog(MainActivity.this);
        waitingDialog.setTitle("我是一个等待Dialog");
        waitingDialog.setMessage("等待中...");
        waitingDialog.setIndeterminate(true);
        waitingDialog.setCancelable(false);
        waitingDialog.show();
    }
    private void showProgressDialog() {
        final int MAX_PROGRESS = 100;
        final ProgressDialog progressDialog =
                new ProgressDialog(MainActivity.this);
        progressDialog.setProgress(0);
        progressDialog.setTitle("我是一个进度条Dialog");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMax(MAX_PROGRESS);
        progressDialog.show();
        /* 模拟进度增加的过程
         * 新开一个线程，每个100ms，进度增加1
         */
        new Thread(new Runnable() {
            @Override
            public void run() {
                int progress= 0;
                while (progress < MAX_PROGRESS){
                    try {
                        Thread.sleep(100);
                        progress++;
                        progressDialog.setProgress(progress);
                    } catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
                // 进度达到最大值后，窗口消失
                progressDialog.cancel();
            }
        }).start();
    }
    private void showInputDialog() {

        final EditText editText = new EditText(MainActivity.this);
        AlertDialog.Builder inputDialog =
                new AlertDialog.Builder(MainActivity.this);
        inputDialog.setTitle("我是一个输入Dialog").setView(editText);
        inputDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,
                                editText.getText().toString(),
                                Toast.LENGTH_SHORT).show();
                    }
                }).show();
    }
    private void showCustomizeDialog() {
        AlertDialog.Builder customizeDialog =
                new AlertDialog.Builder(MainActivity.this);
        final View dialogView = LayoutInflater.from(MainActivity.this)
                .inflate(R.layout.dialog_customize,null);
        customizeDialog.setTitle("我是一个自定义Dialog");
        customizeDialog.setView(dialogView);
        customizeDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 获取EditView中的输入内容
                        EditText edit_text =
                                (EditText) dialogView.findViewById(R.id.btn_dialog8_1);
                        Toast.makeText(MainActivity.this,
                                edit_text.getText().toString(),
                                Toast.LENGTH_SHORT).show();
                    }
                });
        customizeDialog.show();
    }

    private void showListDialog() {
        final String[] items = { "我是男人","我是女人","我是东方不败","我以上都不是" };
        AlertDialog.Builder listDialog =
                new AlertDialog.Builder(MainActivity.this);
        listDialog.setTitle("我是一个列表Dialog");
        listDialog.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // which 下标从0开始
                // ...To-do
                Toast.makeText(MainActivity.this,
                        "你点击了" + items[which],
                        Toast.LENGTH_SHORT).show();
            }
        });
        listDialog.show();
    }

}
